package com.company.project.service.impl;

import com.company.project.dao.*;
import com.company.project.model.Authority;
import com.company.project.model.Role;
import com.company.project.model.Thesis;
import com.company.project.model.User;
import com.company.project.service.UserService;
import com.company.project.core.AbstractService;
import com.company.project.util.Md5;
import com.company.project.util.PageUtil;
import net.sf.jsqlparser.statement.select.FromItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class UserServiceImpl extends AbstractService<User> implements UserService {
    @Resource
    @Autowired
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private AuthorityMapper authorityMapper;

    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private NotificationMapper notificationMapper;

    //前台登录
    @Override
    public User login(String name, String pwd) {

        User num = userMapper.login(name,pwd);
        return num;
    }
    //根据姓名查询个人信息
    @Override
    public User selectMessage(Integer id) {
        User user = userMapper.selectMessage(id);
        List roles = roleMapper.selectRole();
        String phone = user.getPhone();
        phone = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        user.setPhone(phone);
        user.setUlist(roles);
        return user;
    }
    //后台查询根据id查询权限
    @Override
    public List<Map<Object, Object>> enter(Integer id) {
        List<Map<Object, Object>> enter = userMapper.enter(id);
        for(Map<Object, Object> ma : enter){
            if(Integer.parseInt(ma.get("parentid").toString())!= 0){
                List parentid = authorityMapper.Submenu(ma.get("id").toString());
                int i = 1;
                ma.put("subNav",parentid);
            }
        }
        return enter;
    }


    //后台导航栏
    @Override
    public List<User> register(String name, String pwd) {
        List<User> register = userMapper.register(name, pwd);
        for (User user :register){
            Integer integer = notificationMapper.selectMessageQuantity(name, pwd);
            user.setMessageQuantity(integer);
        }
        return register;
    }

    /**
     * 查询后台所有的的用户和用户角色
     * @param pageIndex
     * @return
     */
    @Override
    public PageUtil<User> selectAllUserRole(Integer pageIndex) {
        PageUtil<User> pageUtil = new PageUtil();
        pageUtil.setPageSize(10);
        pageUtil.setCpage(pageIndex);
        pageUtil.setTotalRow(this.selectUserRoleCount());
        List<User> users = userMapper.selectAllUserRole(pageUtil);
        for (User user : users){
            String phone = user.getPhone();
            phone =  phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
            user.setPhone(phone);
        }
        pageUtil.setLists(users);
        return pageUtil;
    }

    /**
     * 后台查询所有用户和用户角色的数量
     * @return
     */
    @Override
    public Integer selectUserRoleCount() {
        return userMapper.selectUserRoleCount();
    }

    /**
     * 后台修改用户和用户角色
     * @param user
     * @return
     */
    @Override
    public Boolean updateUser(User user) {
        Boolean aBoolean1 = userRoleMapper.upUserRole(user);
        Boolean upu = userMapper.UPU(user);
        if (aBoolean1.equals(true) && upu.equals(true)){
            return true;
        }
        return false;
    }

    /**
     * 后台添加用户和角色
     * @param user
     * @return
     */
    @Override
    public Boolean insertUser(User user) {
        Boolean aBoolean = userMapper.insertUser(user);
        Boolean aBoolean1 = userRoleMapper.insertUserRole(user);
        if (aBoolean.equals(true) && aBoolean1.equals(true)){
            return true;
        }
        return false;
    }

    /**
     * 后台删除
     * @return
     */
    @Override
    public Boolean deleteAllUser(Integer id) {
        Boolean aBoolean = userRoleMapper.deleteUserRole(id);
        Boolean aBoolean1 = userMapper.deleteAllUser(id);
        if (aBoolean.equals(true) && aBoolean1.equals(true)){
            return true;
        }
        return false;
    }

    /**
     * 前台修改个人信息
     * @return
     */
    @Override
    public Boolean UPUser(User user) {
        boolean u = userMapper.UPUser(user);
        boolean uR = false;
        if (userRoleMapper.selectUserCount(user.getUid())>0){
            uR = userRoleMapper.upUserRole(user);
            if(u && uR){
                return true;
            }
            return false;
        }
        uR = userRoleMapper.insertUserRole(user);
        if(u && uR){
            return true;
        }
        return false;
    }

    /**
     * 查询密码
     * @return
     */
    @Override
    public User selectCode(User user) {
        return userMapper.selectCode(user);
    }

    /**
     * 修改密码
     * @return
     */
    @Override
    public Boolean upCode(User user) {
        User user1 = this.selectCode(user);
        String s1 = Md5.MD5Encode(user.getPwd());
        String s = Md5.MD5Encode(user.getOPwd());
        if (user1.getPwd().equals(s)){
            user.setPwd(s1);
            Boolean aBoolean = userMapper.upCode(user);
            return aBoolean;
        }else {
            return false;
        }
    }

    @Override
    public Boolean UPU(User user) {
        return userMapper.UPU(user);
    }
    /**
     * 后台修改密码
     */

    /**
     * 修改用户状态
     * @param user
     * @return
     */
    @Override
    public Boolean UPState(User user) {
        return userMapper.UPState(user);
    }

    //验证手机号唯一
    @Override
    public Boolean verifyPhone(String phone) {
        int i = 0;
        i = userMapper.selectPhone(phone);
        if( i == 1){
            return true;
        }
        return false;
    }

    //验证用户名唯一
    @Override
    public Boolean verifyName(String name) {
        int i = 0;
        i = userMapper.selectName(name);
        if( i == 1){
            return true;
        }
        return false;
    }

    //注册
    @Override
    public Boolean addUser(User user) {
        Date date = new Date();
        int i = 0;
        user.setCreateTime(date);
        user.setLastModifyTime(date);
        user.setState(0);
        i= userMapper.insertSelective(user);
        if(i==0){
            return false;
        }
        return true;
    }

    /**
     * 后台修改密码
     * @param user
     * @return
     */
    @Override
    public String adminUP(User user) {
        User user1 = this.UserPhone(user);
        if (user1==null){
            return "用户名和密码不匹配！";
        }else {
            Boolean aBoolean = userMapper.adminUP(user);
            if (aBoolean.equals(true)){
                return "密码修改成功！";
            }else {
                return "密码修改失败！";
            }
        }
    }

    @Override
    public User UserPhone(User user) {
        return userMapper.UserPhone(user);
    }
}
